﻿$(function () {

    $("#save").click(function () {

        $("#submit").hide();

        var month = $("#Month").val();
        var year = $("#Year").val();
        var DaysInMonth = $("#DaysInMonth").val();
        var Latitude = $("#Latitude").val();
        var LocationID = $("#LocationID").val();
        var Longitude = $("#Longitude").val();
        var TimeZone = $("#TimeZone").val();
        var input = new Object();
        input.LocationID = LocationID;
        input.PrayerTimes = new Array();

        for (var day = 1; day <= DaysInMonth; day++) {
            input.PrayerTimes.push({
                Month: month,
                Day: day,
                Fajr: $("#Fajr" + day).val(),
                FajrIqama: $("#FajrIqama" + day).val(),
                Sunrise: $("#Sunrise" + day).val(),
                Duhr: $("#Duhr" + day).val(),
                DuhrIqama: $("#DuhrIqama" + day).val(),
                Asr: $("#Asr" + day).val(),
                AsrIqama: $("#AsrIqama" + day).val(),
                Maghrib: $("#Maghrib" + day).val(),
                MaghribIqama: $("#MaghribIqama" + day).val(),
                Isha: $("#Isha" + day).val(),
                IshaIqama: $("#IshaIqama" + day).val(),
            });
        }

        Ajax({
        url: "/DailyPrayers/SavePrayers",
        data: JSON.stringify(input),
        success: function (data) {
            if (data.IsValid == false) {
                //ShowValidationErrors(data);
            } else if (data.IsSaved == true) {
                ShowMessage("Successfully saved Prayer Times!");
            }
        },
        complete: function () {
            $("#submit").show();
        }
    });
    });

    $("#generateprayer").click(function () {
        if (confirm("This will override all prayer times for this month. Are you sure?")) {

            $("#timestable").fadeOut(1000, function () {
                var month = $("#Month").val();
                var year = $("#Year").val();
                var DaysInMonth = $("#DaysInMonth").val();
                var Latitude = $("#Latitude").val();
                var Longitude = $("#Longitude").val();
                var TimeZone = $("#TimeZone").val();

                for (var day = 1; day <= DaysInMonth; day++) {
                    var date = new Date(year, month, day);
                    prayTimes.setMethod('ISNA');
                    prayTimes.tune({ fajr : -2, maghrib : 2 });
                    var times = prayTimes.getTimes(date, [Latitude, Longitude], TimeZone, "auto", "12hNS");
                    $("#Fajr" + day).val(times["fajr"]);
                    $("#Sunrise" + day).val(times["sunrise"]);
                    $("#Duhr" + day).val(times["dhuhr"]);
                    $("#Asr" + day).val(times["asr"]);
                    $("#Maghrib" + day).val(times["maghrib"]);
                    $("#Isha" + day).val(times["isha"]);
                }

                $("#timestable").fadeIn(1000);
            });
        }
    });

    $('span[id*="r_"]').click(function () {
        var id = $(this).attr("id");
        var v = id.split('_');
        var time = v[1];
        var day = v[2];
        var val = $("#" + time + day).val();
        var DaysInMonth = $("#DaysInMonth").val();
        for (var i = (parseInt(day) + 1); i <= DaysInMonth; i++) {
            $("#" + time + i).val(val);
            $("#" + time + i).effect("pulsate", { times: 1 }, 2000);
        }
    });
});